import {ADDCART} from "@/store/mutation-types";

export default {
    [ADDCART](state, value) {
        state.cartList = value
    },
    changeDelete(state) {
        state.isDelete = !state.isDelete
    },
    delete(state, value) {
        state.cartList = value
    },
    edit(state, value) {
        if (value === 'delete') {
            state.edit = true
        } else {
            state.edit = !state.edit
        }
    },
    pay(state, value) {
        state.orderList = value
    },
    orderListEnd(state){
        state.orderListEnd = state.orderListEnd.concat(state.orderList)
    },
    addAddress(state,value){
        state.userAddress.map(item => {
            if(value.isDefault){
                item.isDefault = false
            }
        })

        let currentDate = new Date()
        value.id = value.id ? value.id : JSON.stringify(currentDate)
        state.userAddress.push(value)
    },
    editAddress(state,value){
        state.userAddress = state.userAddress.map(item =>{
            if(value.isDefault){
                item.isDefault = false
            }

            // if(item.id === value.id){
            //     return value
            // }else{
            //     return item
            // }

            return item.id === value.id ? value : item
        })
    },
    deleteAddress(state,value){
        state.userAddress = state.userAddress.filter(item => {
            return !(item.id === value.id)
        })
        if(value.isDefault){
            state.userAddress[0].isDefault = true
        }
    }
}